package com.zhou.demo.conf;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

@Component
public class MySyslogServe implements ApplicationRunner {
    @Value("${simpleSyslogServer.port_udp}")
    private int port_udp;

    @Override
    public void run(ApplicationArguments args) {
        DatagramSocket ds = null;
        DatagramPacket dp = null;
        try {
            ds = new DatagramSocket(port_udp);
            byte[] buf = new byte[1024];
            dp = new DatagramPacket(buf, buf.length);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        while (true) {
            try {
                ds.receive(dp);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            String msg = new String(dp.getData(), dp.getOffset(), dp.getLength());
            System.out.println(msg);
        }


    }
}
